package com.ricky.web.api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ricky.base.bean.ApiResult;
import com.ricky.base.bean.DbClient;
import com.ricky.web.table.Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;


/**
 * @author Ricky
 * @date 2020/11/30
 */
@Api(tags = "服务配置接口")
@RestController
@RequestMapping("/service")
public class ServiceController {
    @ApiOperation(value = "添加服务")
    @ApiOperationSupport(ignoreParameters = {"service.id"
            , "service.server.host"
            , "service.server.name"
            , "service.server.password"
            , "service.server.port"
            , "service.server.username"
    })
    @PostMapping("/addService")
    public ApiResult<Integer> addService(@RequestBody Service service) throws Exception {
        int res;
        service.setId(UUID.randomUUID().toString());
        try (DbClient dbClient = new DbClient("database.db")) {
            res = dbClient.insert(service);
        }
        return ApiResult.success(res);
    }

    @ApiOperation(value = "修改服务")
    @ApiOperationSupport(ignoreParameters = {
            "service.server.host"
            , "service.server.name"
            , "service.server.password"
            , "service.server.port"
            , "service.server.username"
    })
    @PostMapping("/updateService")
    public ApiResult<Integer> updateService(@RequestBody Service service) throws Exception {
        int res;
        try (DbClient dbClient = new DbClient("database.db")) {
            res = dbClient.update(service);
        }
        return ApiResult.success(res);
    }

    @ApiOperation(value = "删除服务")
    @ApiOperationSupport(includeParameters = {"service.id"})
    @PostMapping("/delService")
    public ApiResult<Integer> delService(@RequestBody Service service) throws Exception {
        int res;
        try (DbClient dbClient = new DbClient("database.db")) {
            res = dbClient.delete(service);
        }
        return ApiResult.success(res);
    }

    @ApiOperation(value = "获取所有服务")
    @PostMapping("/getServiceList")
    public ApiResult<List<Service>> getServiceList() throws Exception {
        List<Service> res;
        try (DbClient dbClient = new DbClient("database.db")) {
            res = dbClient.getAll(Service.class);
        }
//        for (Service re : res) {
//            re.getServer().setPassword(null);
//        }
        return ApiResult.success(res);
    }
}
